package homework7;

import java.util.Arrays;
import java.util.Comparator;


// 定义排序接口
interface Sortable<T> {
    void sort();
}


// 整数数组类，实现 Sortable 接口
class IntArray implements Sortable<int[]> {
    private int[] array;
    private String algorithm;


    // 构造函数
    public IntArray(int[] array, String algorithm) {
        this.array = array;
        this.algorithm = algorithm;
    }


    @Override
    public void sort() {
        switch (algorithm) {
            case "bubble":
                bubbleSort();
                break;
            case "insertion":
                insertionSort();
                break;
            case "quick":
                quickSort(0, array.length - 1);
                break;
            default:
                System.out.println("Invalid sorting algorithm.");
                break;
        }
        printArray();
    }


    // 冒泡排序方法
    private void bubbleSort() {
        int n = array.length;
        for (int i = 0; i < n - 1; i++) {
            for (int j = 0; j < n - i - 1; j++) {
                if (array[j] > array[j + 1]) {
                    int temp = array[j];
                    array[j] = array[j + 1];
                    array[j + 1] = temp;
                }
            }
        }
    }


    // 插入排序方法
    private void insertionSort() {
        int n = array.length;
        for (int i = 1; i < n; ++i) {
            int key = array[i];
            int j = i - 1;


            while (j >= 0 && array[j] > key) {
                array[j + 1] = array[j];
                j = j - 1;
            }
            array[j + 1] = key;
        }
    }


    // 快速排序方法
    private void quickSort(int low, int high) {
        if (low < high) {
            int pi = partition(low, high);


            quickSort(low, pi - 1);
            quickSort(pi + 1, high);
        }
    }


    // 快速排序的分区方法
    private int partition(int low, int high) {
        int pivot = array[high];
        int i = (low - 1);


        for (int j = low; j <= high - 1; j++) {
            if (array[j] < pivot) {
                i++;


                int temp = array[i];
                array[i] = array[j];
                array[j] = temp;
            }
        }
        int temp = array[i + 1];
        array[i + 1] = array[high];
        array[high] = temp;


        return (i + 1);
    }


    // 打印数组元素
    private void printArray() {
        for (int value : array) {
            System.out.print(value + " ");
        }
        System.out.println();
    }
}


// 字符串数组类，实现 Sortable 接口
class StringArray implements Sortable<String[]> {
    private String[] array;
    private String sortType;


    // 构造函数
    public StringArray(String[] array, String sortType) {
        this.array = array;
        this.sortType = sortType;
    }


    @Override
    public void sort() {
        switch (sortType) {
            case "alphabetical":
                Arrays.sort(array);
                break;
            case "length":
                Arrays.sort(array, Comparator.comparing(String::length));
                break;
            case "custom":
                Arrays.sort(array, (s1, s2) -> {
                    // 自定义排序规则示例：按最后一个字符排序
                    char c1 = s1.charAt(s1.length() - 1);
                    char c2 = s2.charAt(s2.length() - 1);
                    return Character.compare(c1, c2);
                });
                break;
            default:
                System.out.println("Invalid sort type.");
                break;
        }
        printArray();
    }


    // 打印数组元素
    private void printArray() {
        for (String value : array) {
            System.out.print(value + " ");
        }
        System.out.println();
    }
}


// 自定义对象类
class CustomObject {
    private int id;
    private String name;


    public CustomObject(int id, String name) {
        this.id = id;
        this.name = name;
    }


    public int getId() {
        return id;
    }


    public String getName() {
        return name;
    }


    @Override
    public String toString() {
        return "CustomObject{" +
                "id=" + id +
                ", name='" + name + '\'' +
                '}';
    }
}


// 自定义对象数组类，实现 Sortable 接口
class CustomObjectArray implements Sortable<CustomObject[]> {
    private CustomObject[] array;
    private String sortBy;


    // 构造函数
    public CustomObjectArray(CustomObject[] array, String sortBy) {
        this.array = array;
        this.sortBy = sortBy;
    }


    @Override
    public void sort() {
        switch (sortBy) {
            case "id":
                Arrays.sort(array, Comparator.comparing(CustomObject::getId));
                break;
            case "name":
                Arrays.sort(array, Comparator.comparing(CustomObject::getName));
                break;
            default:
                System.out.println("Invalid sort by field.");
                break;
        }
        printArray();
    }


    // 打印数组元素
    private void printArray() {
        for (CustomObject value : array) {
            System.out.print(value + " ");
        }
        System.out.println();
    }
}


// 主类，用于测试
public class paidui {
    public static void main(String[] args) {
        // 测试 IntArray
        int[] intArray = {64, 34, 25, 12, 22, 11, 90};
        IntArray intArraySorter = new IntArray(intArray, "bubble");
        intArraySorter.sort();


        // 测试 StringArray
        String[] stringArray = {"apple", "banana", "cherry", "date", "elderberry"};
        StringArray stringArraySorter = new StringArray(stringArray, "alphabetical");
        stringArraySorter.sort();


        // 测试自定义排序规则的 StringArray
        String[] customStringArray = {"apple", "banana", "cherry", "date", "elderberry"};
        StringArray customStringArraySorter = new StringArray(customStringArray, "custom");
        customStringArraySorter.sort();


        // 测试 CustomObjectArray
        CustomObject[] customArray = {
                new CustomObject(3, "Charlie"),
                new CustomObject(1, "Alice"),
                new CustomObject(2, "Bob")
        };
        CustomObjectArray customObjectArraySorter = new CustomObjectArray(customArray, "name");
        customObjectArraySorter.sort();
    }
}